package cn.com.efuture.o2o.backend.mybatis.service;

import cn.com.efuture.o2o.backend.mybatis.entity.O2oGoods;
import cn.com.efuture.o2o.backend.mybatis.entity.O2oGoodsFilter;
import cn.com.efuture.o2o.backend.mybatis.entity.O2oGoodsLog;
import cn.com.efuture.o2o.backend.mybatis.entity.O2oProduct;
import cn.com.efuture.o2o.backend.mybatis.mapper.O2oGoodsMapper;
import cn.com.efuture.o2o.backend.mybatis.mapper.ProductMapper;
import cn.com.efuture.o2o.backend.util.export.CommonExportExcel;
import cn.com.efuture.o2o.backend.util.export.ExportDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl {

    private final ProductMapper productMapper;

    private final O2oGoodsMapper o2oGoodsMapper;

    public ProductServiceImpl(ProductMapper productMapper, O2oGoodsMapper o2oGoodsMapper) {
        this.productMapper = productMapper;
        this.o2oGoodsMapper = o2oGoodsMapper;
    }

    public List<O2oProduct> getProductList(Map<String, Object> map) {
        return productMapper.getProductList(map);
    }

    public O2oProduct getProduct(Map<String, Object> map) {
        return productMapper.getProduct(map);
    }

    @Transactional
    public void updateProduct(O2oProduct o2oProduct, String userName) {
        JSONObject jsonObject = new JSONObject();
        Long itemId = o2oProduct.getItemId();
        jsonObject.put("itemId", itemId);
        //从数据库中获取数据
        O2oProduct product = productMapper.getProduct(jsonObject);
        //从数据库中获取修改之前的状态
        int oldStatus = product.getStatus();

        int newStatus = o2oProduct.getNewStatus();
        String newGoodsName = o2oProduct.getGoodsName();
        String newGoodsEnName = o2oProduct.getGoodsEnName();
        String newCategoryId = o2oProduct.getCategoryId();
        String newBarcode = o2oProduct.getBarcode();
        String newSpec = o2oProduct.getSpec();
        String newUnit = o2oProduct.getUnit();
        Integer newWeight = o2oProduct.getWeight();
        String imageKey1 = o2oProduct.getImageKey1();

        O2oGoods o2oGoods = new O2oGoods();
        if(oldStatus == 1 && newStatus == -1 ||  oldStatus == -1 && newStatus == 1){
            //只要状态含有禁用,商品只修改状态，门店商品只做下架处理
            o2oGoods.setShelved(0);
            o2oProduct = new O2oProduct();
        }else{
            //更新门店商品数据
            o2oGoods.setGoodsName(newGoodsName);
            o2oGoods.setGoodsEnName(newGoodsEnName);
            o2oGoods.setCategoryId(newCategoryId);
            o2oGoods.setBarcode(newBarcode);
            o2oGoods.setSpec(newSpec);
            o2oGoods.setUnit(newUnit);
            o2oGoods.setWeight(newWeight);
        }
        // 修改状态
        if (oldStatus != newStatus) {
            o2oGoods.setStatus(newStatus);
            o2oProduct.setStatus(newStatus);
        }
        //更新商品主档
        o2oProduct.setItemId(itemId);
        productMapper.updateProduct(o2oProduct);

        //同时更新门店商品状态
        o2oGoods.setItemId(itemId);
        o2oGoods.setIsSelfZero(0);
        o2oGoodsMapper.updateO2oGoods(o2oGoods);

        //开始日志处理
        O2oGoodsLog o2oGoodsLog = new O2oGoodsLog();
        if (oldStatus == 1 && newStatus == 1) {
            if (!StringUtils.isEmpty(imageKey1)) {
                if (imageKey1.equals(product.getImageKey1())) {
                    o2oGoodsLog.setIsUpdateImage(0);
                } else {
                    //图片更新 类型修改
                    o2oGoodsLog.setIsUpdateImage(1);
                }
            }
            //商品更新 类型修改
            o2oGoodsLog.setTaskType(2);
        }else if (oldStatus == 0 && newStatus == 1) {
            //新品发布 类型新增
            o2oGoodsLog.setTaskType(1);
        } else if (oldStatus == 1 && newStatus == -1) {
            //商品禁用 类型下架处理
            o2oGoodsLog.setShelved(0);
            o2oGoodsLog.setTaskType(5);
        }
        if (o2oGoodsLog.getTaskType() != null) {
            o2oGoodsLog.setItemId(itemId);
            o2oGoodsLog.setIsSelf(0);
            o2oGoodsLog.setOperator(userName);
            o2oGoodsMapper.insertO2oGoodsLog(o2oGoodsLog);
        }
    }
    
    public List<Map<String, Object>> getO2oGoodsFilterList(Map<String, Object> map) {
        return productMapper.getO2oGoodsFilterList(map);
    }
    
    
    public void insertO2oGoodsFilter(O2oGoodsFilter o2oGoodsFilter,String userName) {
    	JSONObject jsonObject = new JSONObject();
    	String itemCode = o2oGoodsFilter.getItemCode();
        jsonObject.put("itemCode", itemCode);
        int status = productMapper.getProduct(jsonObject).getStatus();
        o2oGoodsFilter.setStatus(status);
    	productMapper.insertO2oGoodsFilter(o2oGoodsFilter);
    }

    public AbstractXlsView exportProductList(final Map<String, Object> map) {
        // execl 表头数据
        Object[][] title = {
                {"*商品编码", "itemCode", 0, null}, {"商品名称", "goodsName", 0, null}, {"英文名称", "goodsEnName", 0, null},
                {"商品状态", "status", 0, null},{"分类编码", "categoryId", 0, null}, {"分类名称", "categoryName", 0, null}, {"商品条码", "barcode", 0, null},
                {"商品规格", "spec", 0, null},{"销售单位", "unit", 0, null}, {"包装数量", "pkSpec", 0, null}, {"属性", "itemType", 0, null},
                {"长(厘米)", "length", 0, null},{"宽(厘米)", "width", 0, null},{"高(厘米)", "height", 0, null},{"重量(克)", "weight", 0, null},
                {"商品描述", "memo", 0, null}};

        return new CommonExportExcel(title, "商品主档表", new ExportDataSource() {
            @Override
            public List<Map<String, Object>> getData(int pageSize, int page) {
                return null;
            }
        });
    }


}
